package org.nicocube.airain.gwt.client.ui.item;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

public class TableOfItem<R extends Item> {

	/**
	 * Default CSS class name for {@link TableOfItem} 
	 */
	private static final String DaGrid = "daGrid";

	private static final String DaGridPanel = "daGridPanel";
	
	/**
	 * The {@link TableHeader} object associated with this table of item   
	 */
	private TableHeader header;
	
	/**
	 * The CSS class name applied to the rendered {@link TableOfItem}
	 */
	private String className;

	/**
	 * The secundary CSS class name applied to the rendered {@link TableOfItem}
	 */
	private String additionnalClassName;
	
	/**
	 * 
	 */
	private List<R> rows;
	
	public TableOfItem(TableHeader header) {
		this(header,DaGrid,null);
	}
	
	public TableOfItem(TableHeader header,String className,String additionnalClassName) {
		this.className = className;
		this.header = header;
		this.rows = new LinkedList<R>();
		this.additionnalClassName=additionnalClassName;
	}
	
	public TableOfItem<R> addRows(R row) {
		rows.add(row);
		return this;
	}
	
	public Widget render() {
		FlexTable result = new FlexTable();
		result.setCellPadding(0);
		result.setCellSpacing(1);
		result.setStylePrimaryName(className);
		if (additionnalClassName!=null) result.addStyleName(additionnalClassName);
		
		header.render(result);
		int r=1;
		for (R row : rows) {
			row.render(r,result);
			r++;
		}
		DecoratorPanel dp = new DecoratorPanel();
		dp.add(result);
		dp.addStyleName(DaGridPanel);
		return dp;
	}
}
